package com.gable.exhibition.service.impl;

import com.gable.common.util.ReturnResultUtil;
import com.gable.exhibition.dao.EventDao;
import com.gable.exhibition.dao.FileDao;
import com.gable.exhibition.model.Event;
import com.gable.exhibition.model.File;
import com.gable.exhibition.service.EventService;
import com.gable.exhibition.util.ExceptionUtil;
import com.gable.exhibition.util.MessageUtil;
import com.gable.exhibition.view.EventView;
import com.gable.exhibition.view.FileView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by mj on 2018/2/26.
 */
@Service
public class EventServiceImpl implements EventService{

    @Autowired
    EventDao eventDao;

    @Autowired
    FileDao fileDao;

    @Override
    @Transactional
    public ReturnResultUtil addEvent(EventView eventView) {
        int row = eventDao.insertSelective(eventView);
        if(row == 0)
            return new ReturnResultUtil(500, MessageUtil.FAILED);
        List<FileView> fileList = eventView.getFileList();
        if(fileList != null && fileList.size() > 0){
            for(FileView fileView : fileList){
                row = fileDao.insertSelective(fileView);
                if(row == 0)
                    throw new ExceptionUtil(500,MessageUtil.UPLOADEXCEPTION,fileView);
            }
        }
        return new ReturnResultUtil(200, MessageUtil.SUCCESSFUL);
    }

    @Override
    public ReturnResultUtil eventList(EventView eventView) {
        List<Event> events = eventDao.eventList(eventView);
        return new ReturnResultUtil(200,MessageUtil.QUERYSUCCESS,events);
    }

    @Override
    @Transactional
    public ReturnResultUtil addEventForWeChat(EventView eventView) {
        int row = eventDao.insertSelective(eventView);
        if(row == 0)
            return new ReturnResultUtil(500, MessageUtil.FAILED);
        List<String> fileIds = eventView.getFileIds();
        if(fileIds != null && fileIds.size() > 0){
            for(String fileId : fileIds){
                File file = new File();
                file.setFileId(Long.parseLong(fileId));
                file.setKeyId(eventView.getEventId());
                row = fileDao.updateByPrimaryKeySelective(file);
                if(row == 0)
                    throw new ExceptionUtil(500,MessageUtil.UPLOADEXCEPTION,file);
            }
        }
        return new ReturnResultUtil(200, MessageUtil.SUCCESSFUL);
    }

    @Override
    @Transactional
    public ReturnResultUtil deleteEvent(EventView eventView) {
        eventView.setIsDel(1);
        int i = eventDao.updateByPrimaryKeySelective(eventView);
        if(i > 0){
            File file = new File();
            file.setKeyType(1405L); //事件
            file.setKeyId(eventView.getEventId());
            file.setIsDel(1);
            fileDao.deleteByKeyTypeKeyId(file);
        }
        return new ReturnResultUtil(200, MessageUtil.SUCCESSFUL);
    }
}
